﻿using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HUNTERS.Models;
using HUNTERS.CORE;
using HUNTERS.CORE.Models.Request;
using HUNTERS.CORE.Models.Response;
using Newtonsoft.Json;
using System.Windows.Controls;
using MetroFramework;

namespace HUNTERS
{
    public partial class FormInfo : MetroForm
    {



        private Users _user;
        private Services _services;
        public static List<GuessQuestions> guessQuestionsItems = new List<GuessQuestions>();
        private Main _main;
        private DataGridViewRow _currentRow;

        public FormInfo(Main main)
        {
            InitializeComponent();
            this._main = main;
            this.AcceptButton = metroButtonSubmmit;
            this.CancelButton = metroButton_Cancel;
        }

        private static int testTime = 3;
        

        private void metroButtonSubmmit_Click(object sender, EventArgs e)
        {

            var qitem =metroComboBox_Question.SelectedItem as QuestionsItem;
            guessQuestionsItems.Clear();
            GuessQuestions GuessQuestions = new GuessQuestions();
            GuessQuestions.id = qitem.id;
            GuessQuestions.question = metroComboBox_Question.Text;
            GuessQuestions.answer = metroTextBox_Anwser.Text;
            GuessQuestions.number = qitem.number;
            GuessQuestions.level = Convert.ToInt32(1);
            guessQuestionsItems.Add(GuessQuestions);
            if (_services.GuessQuestions(guessQuestionsItems))
            {

                _currentRow.Cells[3].Style.ForeColor = Color.Green;
                
                if (_main.RestQuestion(_user, _services))
                {
                    _user = null;
                    _services = null;
                    this.Hide();

                }

            }
            else
            {

                MetroMessageBox.Show(this,"您的答案与存档的信息不否！","密保尝试",MessageBoxButtons.OK,100);

                if (testTime == 1)
                {                    
                    this.Hide();
                   
                }
                testTime--;
            }
            this.TopMost = true;
           
        }



        internal void Update(DataGridViewRow currentRow, Services service)
        {
            if(testTime!=3)
                testTime = 3;
            _currentRow = currentRow;
            _user = currentRow.DataBoundItem as Users;
            _services = service;
            var question =  service.currentQuestions.questions as System.ComponentModel.BindingList<QuestionsItem>;
            metroTextBox_UserOtherInfo.Text = string.Format("当前账号[{0}],信息资料[{1}]", _user.UserID, _user.GSOtherInfo());
            metroComboBox_Question.DataSource = question;
            metroComboBox_Question.ValueMember = "question";
            metroComboBox_Question.SelectedIndex = 0;


        }

        private void metroButton1_Click(object sender, EventArgs e)
        {
            
          
            this.Hide();
        }

        private void FormInfo_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
            {
                _main.Enabled = false;
            }
            else
            {
                _main.Enabled = true;
            }
        }

        private void FormInfo_Load(object sender, EventArgs e)
        {

        }

        private void FormInfo_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter)//如果输入的是回车键  
            //{
            //    metroButtonSubmmit_Click(sender, e);

            //}
        }
    }
}
